package hz;


import com.alibaba.fastjson.JSONObject;
import javax.naming.Context;
import java.sql.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2016/7/17.
 */
public class DBUtil {
    static String  leixing="test";
    public static Connection getPoorConnection() {
        Setting.readProperties("default1.properties");
        Connection conn=null;

        Context ctx= null;
                try {
                    Class.forName("com.mysql.jdbc.Driver");
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
                try {

                    conn = DriverManager.getConnection(Setting.getProperty("url"), Setting.getProperty("user"), Setting.getProperty("password"));
                    return conn;
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            return conn;
    }



    public static int getKey(String ziduan,String biaoming){
        Connection conn= DBUtil.getPoorConnection();
        String sql="";
        int result=1;
        ResultSet rs=null;
        sql="select IFNULL(MAX("+ziduan+"),0)+1 from "+biaoming;
        PreparedStatement ps=null;
        try {
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            if(rs.next()){
                result=rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                ps.close();
                conn.close();
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return result;
        }



}


    public static String getMsg(String m){
        JSONObject json= JSONObject.parseObject(m);
        return json.getString("message");
    }

    public static RuleBean getRule1(String id1) throws SQLException {
        RuleBean ruleBean=null;
        Connection con = DBUtil.getPoorConnection();
        String sql="SELECT id,timerange,number,samepol,diffpol FROM rule WHERE id="+id1;
        PreparedStatement pre = con.prepareStatement(sql);
        ResultSet rs = pre.executeQuery();
        if (rs.next()){
            ruleBean=new RuleBean();
            int id=rs.getInt("id");
            ruleBean.setId(id);
            ruleBean.setTimerange(rs.getInt("timerange"));
            ruleBean.setNumber(rs.getInt("number"));
            ruleBean.setSamepol(rs.getString("samepol"));
            ruleBean.setDiffpol(rs.getString("diffpol"));
            ruleBean.setTiaojian(DBUtil.gettiaojiao(id));

        }
        return ruleBean;
    }


    public static List<RuleBean> getRule2(String id1) throws SQLException {
        RuleBean ruleBean=null;
        Connection con = DBUtil.getPoorConnection();
        String sql="SELECT id,timerange,number,samepol,diffpol FROM rule WHERE id="+id1;
        PreparedStatement pre = con.prepareStatement(sql);
        ResultSet rs = pre.executeQuery();
        if (rs.next()){
            ruleBean=new RuleBean();
            int id=rs.getInt("id");
            ruleBean.setId(id);
            ruleBean.setTimerange(rs.getInt("timerange"));
            ruleBean.setNumber(rs.getInt("number"));
            ruleBean.setSamepol(rs.getString("samepol"));
            ruleBean.setDiffpol(rs.getString("diffpol"));
            ruleBean.setTiaojian(DBUtil.gettiaojiao(id));

        }
        List<RuleBean> list=new ArrayList<>();
        list.add(ruleBean);
        return list;
    }





    public static List<RuleBean> getRule(String id1) throws SQLException {
        String sql="select ruleid,leixing from cj where id="+id1;
        List<RuleBean> list=new ArrayList<>();

        Connection con = DBUtil.getPoorConnection();
        PreparedStatement pre = con.prepareStatement(sql);
        ResultSet rs = pre.executeQuery();
        String id="";

        if (rs.next()){
            id=rs.getString("ruleid");
            leixing=rs.getString("leixing");
        }
        String[] r = id.split(",");
        for (String i:r){
            RuleBean ruleBean = getRule1(i);
            list.add(ruleBean);
        }

         return list;

    }

    public static String getLeixing() {
        return leixing;
    }

    public static void setLeixing(String leixing) {
        DBUtil.leixing = leixing;
    }

    public static List<TiaojianBean> gettiaojiao(int id) throws SQLException {
        Connection rs = getPoorConnection();
        TiaojianBean tiaojianBean=null;
        List<TiaojianBean> list=new ArrayList<>();
        String sql="SELECT ziduan,caozuofu,zhi FROM `condition` WHERE ruleid="+id;
        PreparedStatement rr = rs.prepareStatement(sql);
        ResultSet result = rr.executeQuery();
       /* if(result.next()){

        while (result.next()){
                tiaojianBean.setZiduan(result.getString("ziduan"));
                tiaojianBean.setCaozuofu(result.getString("caozuofu"));
                tiaojianBean.setZhi(result.getString("zhi"));
                list.add(tiaojianBean);

        }}*/
        if(result.next()) {
            do {
                tiaojianBean=new TiaojianBean();
                tiaojianBean.setZiduan(result.getString("ziduan"));
                tiaojianBean.setCaozuofu(result.getString("caozuofu"));
                tiaojianBean.setZhi(result.getString("zhi"));
                list.add(tiaojianBean);
            }while(result.next());
        } else {
            System.out.println("ResultSet is null or empty!");
        }
        return list;
    }


    public static List<RuleBean> getlist(){
        List<RuleBean> list=new ArrayList<>();
        RuleBean ruleBean=new RuleBean();
        //ruleBean.setGuanjianci("");
        List<TiaojianBean> li1=new ArrayList<>();
        TiaojianBean tiaojianBean=new TiaojianBean();
        tiaojianBean.setCaozuofu("=");
        tiaojianBean.setZhi("172.18.18.11");
        tiaojianBean.setZiduan("src");
        li1.add(tiaojianBean);
        ruleBean.setTiaojian(li1);
        ruleBean.setMktime(new Date(new Date().getTime()-1000*60*60*24));
        ruleBean.setId(1);
        ruleBean.setSamepol("src");
        ruleBean.setDiffpol("dst");
        ruleBean.setNumber(1);
        ruleBean.setTimerange(15);
        list.add(ruleBean);
        return list;
    }

    public static void main(String[] args) throws SQLException {

        List<RuleBean> ce = DBUtil.getRule2("18");
        System.out.println(ce);
    }
}
